package server.handler;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.json.JSONObject;
import server.database.RegistrationDatabase;
import server.entity.Registration;
import server.util.Base64Util;
import server.util.MD5Util;
import server.util.SessionUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

//session logout
//public class LogoutHandler implements HttpHandler {
//
//
//    @Override
//    public void handle(HttpExchange exchange) throws IOException {
//
//        Headers headers = exchange.getRequestHeaders();
//
//        String contentType = headers.get("Content-Type").get(0);
//        if(contentType.equalsIgnoreCase("application/json")){
//            if (exchange.getRequestMethod().equalsIgnoreCase("POST") || exchange.getRequestMethod().equalsIgnoreCase("GET")) {
//                if(!Handler.authentication(exchange)){
//                    Handler.errorHandler(exchange, "Please login or register first");
//                }
//                String inputData;
//                try{
//                    inputData = Handler.getRequestData(exchange);
//                }catch (IOException e){
//                    Handler.errorHandler(exchange, e);
//                    return;
//                }
//                JSONObject jsonObject = new JSONObject(inputData);
//                //attribute validation
//                if(!jsonObject.has("username")){
//                    Handler.errorHandler(exchange, "username is required");
//                    return;
//                }
//
//                String userName = jsonObject.getString("username");
//
//
//                //remove session
//                String session = exchange.getRequestHeaders().get("session").get(0);
//                try {
//                    Handler.removeSession(session);
//                } catch (Exception e) {
//                    Handler.errorHandler(exchange, e);
//                    return;
//                }
//
//                Handler.sendResponse(exchange, "logout success");
//
//            }else{
//                //Code for handling other requests
//                Handler.errorHandler(exchange,"Unsupported request method");
//            }
//        }else {
//            Handler.errorHandler(exchange, "Unsupported request contentType");
//        }
//    }
//}

//Authorization logout
public class LogoutHandler implements HttpHandler {


    @Override
    public void handle(HttpExchange exchange) throws IOException {

        Headers headers = exchange.getRequestHeaders();

        String contentType = headers.get("Content-Type").get(0);
        if(contentType.equalsIgnoreCase("application/json")){
            if (exchange.getRequestMethod().equalsIgnoreCase("POST") || exchange.getRequestMethod().equalsIgnoreCase("GET")) {
                Handler.sendResponse(exchange, "logout success");

            }else{
                //Code for handling other requests
                Handler.errorHandler(exchange,"Unsupported request method");
            }
        }else {
            Handler.errorHandler(exchange, "Unsupported request contentType");
        }
    }
}

